package util;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

public class HttpClientUtil {

	public static InputStream doPost(String url, HttpEntity entity) {
		DefaultHttpClient client = new DefaultHttpClient();
		HttpHost proxy = new HttpHost("172.20.230.5", 3128);
		client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
		HttpPost post = new HttpPost(url);
		
		try {
			post.setEntity(entity);
			ResponseHandler<String> responseHandler = new BasicResponseHandler();
	            
	        String responseBody = client.execute(post, responseHandler);
			System.out.println(responseBody);
			return new ByteArrayInputStream(responseBody.getBytes());
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			client.getConnectionManager().shutdown();
		}
		return null;
	}

}
